package com.yc.hotel.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.yc.hotel.dto.UserInfoDTO;
import com.yc.hotel.entity.RoomInfo;
import com.yc.hotel.enums.ResultEnum;
import com.yc.hotel.service.IRoomInfoService;
import com.yc.hotel.user.feign.IUserInfoFeignClient;
import com.yc.hotel.util.ConstantInfo;
import com.yc.hotel.util.StringUtil;
import com.yc.hotel.vo.ResultVO;

@RestController
@RequestMapping("/room")
public class RoomInfoController {

	@Autowired
	private IRoomInfoService roomInfoServiceImpl;
	
	@Autowired
	private IUserInfoFeignClient userInfoFeignController;
	
	@PostMapping("/add")
	public ResultVO add(RoomInfo rf, MultipartFile picss, MultipartFile photos, HttpSession session) {

		Object obj = session.getAttribute(ConstantInfo.CURRENTMEMBERINFO);
		if (obj == null) {
			return new ResultVO(ResultEnum.LOGIN_ERROR);
		}
		UserInfoDTO user = (UserInfoDTO)obj;
		int uno = user.getUno();
		String savePath = "";
		File dest = null;
		if (picss != null ) {
			String picStr = "";
			try {
				// 重命名图片
				savePath = ConstantInfo.UPLOADPATH + "/pics" + "/" + System.currentTimeMillis()+ "_"
						+ picss.getOriginalFilename();
				dest = new File(ConstantInfo.basePath, savePath);
				picss.transferTo(dest);
				
				picStr += savePath;
				
			} catch (IllegalStateException | IOException e) {
				e.printStackTrace();
			}
			rf.setPics(picStr);
		}
		if (photos != null ) {
			String picStr = "";
			try {
				// 重命名图片
				savePath = ConstantInfo.UPLOADPATH  + "/photo" + "/" + System.currentTimeMillis()+ "_"
						+ photos.getOriginalFilename();
				dest = new File(ConstantInfo.basePath, savePath);
				photos.transferTo(dest);
				
				picStr += savePath;
				
			} catch (IllegalStateException | IOException e) {
				e.printStackTrace();
			}
			rf.setPhoto(picStr);
		}
		rf.setUno(uno);
		System.out.println(rf.getPics());
		System.out.println(rf.getPhoto());
		int result = roomInfoServiceImpl.add(rf);
		System.out.println("add result: " + result);
		return new ResultVO(ResultEnum.SUCCESS, result);
	}


	@PostMapping("/update")
	public ResultVO update(@RequestParam Map<String, Object> map){
		int result = roomInfoServiceImpl.update(map);
		System.out.println("updayte result: " + result);
		return new ResultVO(ResultEnum.SUCCESS, result);
	};
	
	@PostMapping("/updateStatus")
	public ResultVO updateStatus(@RequestParam Map<String, Object> map){
		int result = roomInfoServiceImpl.updateStatus(map);
		System.out.println("updateStatus result: " + result);
		return new ResultVO(ResultEnum.SUCCESS, result);
	};
	
	@PostMapping("/findByPage")
	private ResultVO findByPage(@RequestParam Map<String, Object> map) {
		map.put("page", 1);
		map.put("rows", 3);
		List<RoomInfo> list = roomInfoServiceImpl.findByPage(map);
		for(RoomInfo room : list) {
			int uno = room.getUno();
			UserInfoDTO user = userInfoFeignController.findByUno(uno);
			room.setNickName(user.getNickName());
			room.setTel(user.getTel());
		}
		
		return new ResultVO(ResultEnum.SUCCESS, list);
	};

	@PostMapping("/findByRno")
	private ResultVO findByRno(String rno) {
		RoomInfo room = roomInfoServiceImpl.findByRno(rno);
		int uno = room.getUno();
		UserInfoDTO user = userInfoFeignController.findByUno(uno);
		room.setNickName(user.getNickName());
		room.setTel(user.getTel());
		return new ResultVO(ResultEnum.SUCCESS, room);
	};

	@PostMapping("/finds")
	private ResultVO finds(@RequestParam Map<String, Object> map) {
		
		List<RoomInfo> list = roomInfoServiceImpl.finds(map);
		for(RoomInfo room : list) {
			int uno = room.getUno();
			UserInfoDTO user = userInfoFeignController.findByUno(uno);
			room.setNickName(user.getNickName());
			room.setTel(user.getTel());
		}
		return new ResultVO(ResultEnum.SUCCESS, list);
	};
	@PostMapping("/change")
	private ResultVO change(@RequestParam Map<String, Object> map) {
		if(!StringUtil.checkNull(map.get("province"), map.get("city"), map.get("address"))) {
			String addr = "" + map.get("province") + map.get("city") + map.get("address");
			map.put("addr", addr);
		}	
		
		List<RoomInfo> list = roomInfoServiceImpl.finds(map);
		for(RoomInfo room : list) {
			int uno = room.getUno();
			UserInfoDTO user = userInfoFeignController.findByUno(uno);
			room.setNickName(user.getNickName());
			room.setTel(user.getTel());
		}
		return new ResultVO(ResultEnum.SUCCESS, list);
	};

	@GetMapping("/findIndex")
	private Map<String, Object> findIndex() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", 1);
		map.put("tno", 101);
		List<RoomInfo> sale = roomInfoServiceImpl.findIndex(map);
		for(RoomInfo room : sale) {
			int uno = room.getUno();
			UserInfoDTO user = userInfoFeignController.findByUno(uno);
			room.setNickName(user.getNickName());
			room.setTel(user.getTel());
		}
		
		
		map.put("tno", 102);
		List<RoomInfo> rent = roomInfoServiceImpl.findIndex(map);
		for(RoomInfo room : rent) {
			int uno = room.getUno();
			UserInfoDTO user = userInfoFeignController.findByUno(uno);
			room.setNickName(user.getNickName());
			room.setTel(user.getTel());
		}
		
		
		map.remove("tno");
		map.put("sale", sale);
		map.put("rent", rent);
		return map;
	};
}
